// Package requests 处理请求数据和表单验证
package requests

import (
	"mime/multipart"

	"github.com/gofiber/fiber/v2"
	"github.com/thedevsaddam/govalidator"
)

type UploadRequest struct {
	Avatar *multipart.FileHeader `valid:"avatar" json:"avatar"`
	Type   string                `valid:"type" json:"type"`
}

// https://pkg.go.dev/github.com/thedevsaddam/govalidator@v1.9.10#section-readme
func ValidateUpload(c *fiber.Ctx, request *UploadRequest) map[string][]string {

	if form, err := c.MultipartForm(); err == nil {
		request.Avatar = form.File["avatar"][0]
		request.Type = "image"
	}

	rules := govalidator.MapData{
		"file:avatar": []string{"ext:png,jpg,jpeg", "size:20971520"},
		"type":        []string{"required", "in:image"},
	}

	messages := govalidator.MapData{}

	return Validate(request, rules, messages)
}
